/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.servlet;

import br.com.foxline.model.Annex;
import br.com.foxline.service.AnnexService;
import java.io.IOException;
import java.io.OutputStream;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author roniere
 */
@WebServlet(name = "AnnexShowServlet", urlPatterns = "/show/*")
public class AnnexShowServlet extends HttpServlet {

    @EJB
    AnnexService annexService;
    private static final long serialVersionUID = 201011231103L;

    public AnnexShowServlet() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        OutputStream out;
        
        try {
            String id = request.getParameter("annex");
            String content = request.getParameter("annexContent");
            System.err.println("ID: "+id);
            Annex annex = new Annex();
            if(id != null || !id.equals("")){
                annex = annexService.find(Long.parseLong(id));
                out = response.getOutputStream();
                response.setContentType(annex.getType());
                System.err.println("Content: "+annex.getFileByte());
                response.getOutputStream().write(annex.getFileByte());
            }else{
                out = response.getOutputStream();
                response.setContentType("image/jpg");
//                response.getOutputStream().write(Base64.decode(content));
            }

            if (out != null) {
                out.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
